/**ai出牌顺子 */
var _ = require('../../../FrameWork/we_underscore');
var FindSameCardValueTool = require('../../FindTool/FindSameCardValueTool');
var EumPkGroup = require('../../Enum/EumPkGroup');
var AIGetShunZi = module.exports = {};

AIGetShunZi.doMore = function(_lastCardInfo, _cardsInfo) {
    var lastCards = _lastCardInfo.cardsInfo
    var lastShunZiCnt = _.size(lastCards); //上个玩家出的顺子个数
    var cardCnt = _.size(_cardsInfo);
    if (lastShunZiCnt > cardCnt) {
        return {
            isFind: false
        };
    }
    lastCards = _.sortBy(lastCards, function(_cardInfo) {
        return _cardInfo.value;
    });
    var lastFristValue = lastCards[0].value;
    var lastEndValue = lastCards[lastShunZiCnt - 1].value;

    /***最大的顺子了连到A了*/
    if (14 == lastEndValue) {
        return {
            isFind: false
        };
    }
    /**过滤掉比顺子小的牌和王*/
    var canCardsInfo = _.filter(_cardsInfo, function(_cardInfo) {
        return _cardInfo.value > lastFristValue && 5 != _cardInfo.type;
    });

    if (lastShunZiCnt > _.size(canCardsInfo)) {
        return {
            isFind: false
        };
    }
    //可能出现的组数量
    var groupCnt = 14 - lastEndValue;
    var needFristValue = lastFristValue + 1;

    var resultMore = {
        isFind: false,
        allGetCardsGroup: []
    };
    for (var i = needFristValue; i < (needFristValue + groupCnt); ++i) {
        var result = this.find(i, i + lastShunZiCnt, canCardsInfo);
        if (result.isFind) {
            resultMore.allGetCardsGroup.push(result.recommendCards);
        }
    }
    resultMore.isFind = _.size(resultMore.allGetCardsGroup) > 0;
    resultMore.type = EumPkGroup.STRAIGHT;
    return resultMore;
};
/***AI出牌
 * _cardsInfo:[{}];
 */
AIGetShunZi.do = function(_lastCardInfo, _cardsInfo) {
    var resultMore = this.doMore(_lastCardInfo, _cardsInfo);
    if (resultMore.isFind) {
        resultMore.recommendCards = resultMore.allGetCardsGroup[0];
    }
    return resultMore;
};

AIGetShunZi.find = function(cardMin, cardMax, _cardsInfo) {
    var recommendCards = [];
    for (var value = cardMin; value < cardMax; ++value) {
        var findCardInfo = _.find(_cardsInfo, function(_cardInfo) {
            return _cardInfo.value == value;
        });
        if (!findCardInfo) {
            return {
                isFind: false,
            };
        }
        recommendCards.push(findCardInfo);
    }
    return {
        isFind: true,
        type: EumPkGroup.STRAIGHT,
        recommendCards: recommendCards, //推荐组
        allGetCardsGroup: [] //所有组 
    };
};